<?php
/**
 * @title 29.两数相除
 * @author start2004
 */

class Solution {

    /**
     * @param Integer $dividend
     * @param Integer $divisor
     * @return Integer
     */
    function divide($dividend, $divisor) {
        /**
         * @since 2021-10-12 被除数dividend=0, 避免正负判断
         */
        if($dividend == 0){
            return 0;
        } else {}

        /**
         * @since 2021-10-12 正负比较
         */
        if(($dividend > 0 AND $divisor > 0) OR ($dividend < 0 AND $divisor < 0)){
            $sign = 1;
        } else {
            $sign = -1;
        }

        /**
         * @since 2021-10-13 最大数, 最小数
         */
        $num_max = pow(2, 30);
        $num_max = $num_max-1+$num_max;
        $num_min = 0-$num_max-1;

        /**
         * @since 2021-10-13 除数divisor溢出
         */
        if($divisor == $num_min){
            if($dividend == $num_min){
                return 1;
            } else {
                return 0;
            }
        } else {}
        $divisor = abs($divisor);

        /**
         * @since 2021-10-12 结果补值
         */
        $add = 0;

        /**
         * @since 2021-10-13 被除数dividend溢出
         */
        if($dividend == $num_min){
            /**
             * @since 2021-10-13 防止溢出
             */
            if($divisor == 1){
                if($sign == 1){
                    return $num_max;
                } else {
                    return $num_min;
                }
            } else {}
            
            /**
             * @since 2021-10-12 -2^31取正溢出
             */
            $dividend += $divisor;
            $dividend = abs($dividend);
            $add = 1;
        } else {
            $dividend = abs($dividend);
        }

        /**
         * @since 2021-10-12 转字符串
         */
        $dividend = strval($dividend);
        $divisor = strval($divisor);
        $len1 = strlen($dividend);
        $len2 = strlen($divisor);
        // echo "add={$add}, sign={$sign}", PHP_EOL;

        /**
         * @since 2021-10-12 商-数组
         */
        $numArray = [];

        /**
         * @since 2021-10-12 初始判断
         */
        if($len1 < $len2){
            $numArray[] = 0;
        } else {
            $a = substr($dividend, 0, $len2-1);

            for($i=$len2-1; $i<$len1; $i++){
                $a = $a . $dividend[$i];
                $b = 0;
                while($a >= $divisor){
                    $a -= $divisor;
                    $b++;
                }
                
                if($b == 0 AND count($numArray) == 0){
                } else {
                    $numArray[] = $b;
                }
            }

        }

        /**
         * @since 2021-10-12 商
         */
        $result = (implode("", $numArray) + $add) * $sign;

        /**
         * @return
         */
        return $result;
    }
}



/**
 * @url http://127.0.0.1/leetcode/202007/2020.07.03_8.php
 */
$datas = [
    [10, 3], // 3
    [101, 2], // 50
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->divide($data[0], $data[1]);
    var_dump($result);
    echo str_repeat("<br>", 3);
}
$xhprof->end();
